package com.llkj.contractmanagement.main;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.jaeger.library.StatusBarUtil;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.login.LoginActivity;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.ObserverCallBack;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.AppManager;
import com.llkj.contractmanagement.utils.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

public abstract class BaseActivity extends AppCompatActivity implements ObserverCallBack, TitleBar.TitleBarClickListener {
    protected ProgressDialog waitDialog;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(getLayoutResId());//把设置布局文件的操作交给继承的子类
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        setStatusBar();
    }

    protected void setStatusBar() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.title_bar_color));
    }

    /**
     * 返回当前Activity布局文件的id
     *
     * @return
     */
    protected abstract int getLayoutResId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        OkHttpUtils.getInstance().cancelTag(this);//取消以Activity.this作为tag的请求
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        dismissWaitDialog();
    }

    @Override
    public void onFailureHttp(String message) {
        dismissWaitDialog();
        ToastUtil.makeShortText(this, message);
    }

    @Override
    public void onError(Exception error) {
        dismissWaitDialog();
    }

    @Override
    public void onConflict(String message) {
        dismissWaitDialog();
        showConflictDialog(message);
    }

    @Override
    public void onBefore(int resultCode) {
        showWaitDialog();
    }

    @Override
    public void leftClick() {
        this.finish();
    }

    @Override
    public void rightClick() {

    }

    /**
     * 全局等待对话
     */
    public void showWaitDialog() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (waitDialog == null || !waitDialog.isShowing()) {
                    waitDialog = new ProgressDialog(BaseActivity.this, R.style.MyDialogStyle1);
                    waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    waitDialog.setCanceledOnTouchOutside(false);
                    View view = getLayoutInflater().inflate(R.layout.dialog_waitting, null);
                    waitDialog.show();
                    waitDialog.setContentView(view);
                }
            }
        });

    }

    public void dismissWaitDialog() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (waitDialog != null && waitDialog.isShowing()) {
                        waitDialog.dismiss();
                        waitDialog = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void showConflictDialog(String message) {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage(message)
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        UserInfoBean.getInstance().clearUserInfo();
                        AppManager.getAppManager().finishAllActivityAndExit();
                        UserInfoBean.getInstance().clearUserInfo();
                        JPushInterface.setAlias(BaseActivity.this, "", null);
                        Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                }).setCancelable(false).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);

    }
}
